/*
 * Basic CDBUS Communication Example
 * 
 * This example demonstrates basic CDBUS communication between two Arduino boards.
 * 
 * Hardware connections:
 * - Connect TX1 to RX2 and RX1 to TX2
 * - Connect GND together
 * - Use RS-485 transceivers for long distance communication
 */

#include <SimpleCDBUS.h>

// Create CDBUS instance
SimpleCDBUS cdbus;

// Device address - change this for each device
const uint8_t DEVICE_ADDRESS = 0x01;

// Destination address for communication
const uint8_t DEST_ADDRESS = 0x02;

void setup() {
    Serial.begin(115200);
    while (!Serial) {
        ; // wait for serial port to connect
    }
    
    Serial.println("SimpleCDBUS Basic Example");
    Serial.print("Device Address: 0x");
    Serial.println(DEVICE_ADDRESS, HEX);
    
    // Initialize CDBUS with Serial1 (pins 18/19 on Arduino Mega)
    cdbus.begin(&Serial1, DEVICE_ADDRESS);
    
    // Optional: Set baud rate
    cdbus.setBaudRate(115200);
    
    Serial.println("CDBUS initialized");
    Serial.println("Ready to send/receive data");
    Serial.println("Commands:");
    Serial.println("  's' - Send test data");
    Serial.println("  'r' - Read received data");
    Serial.println("  'a' - Check availability");
}

void loop() {
    cdbus.update();  // Process incoming data
    
    if (Serial.available()) {
        char cmd = Serial.read();
        
        switch (cmd) {
            case 's':
            case 'S':
                sendTestData();
                break;
                
            case 'r':
            case 'R':
                readReceivedData();
                break;
                
            case 'a':
            case 'A':
                checkAvailability();
                break;
                
            default:
                Serial.println("Unknown command");
                break;
        }
    }
    
    // Automatically process received data
    if (cdbus.available()) {
        uint8_t src;
        uint8_t buffer[64];
        uint8_t len = cdbus.read(&src, buffer, sizeof(buffer));
        
        if (len > 0) {
            Serial.print("Received data from 0x");
            Serial.print(src, HEX);
            Serial.print(": ");
            
            for (uint8_t i = 0; i < len; i++) {
                Serial.print("0x");
                if (buffer[i] < 0x10) Serial.print("0");
                Serial.print(buffer[i], HEX);
                Serial.print(" ");
            }
            Serial.println();
        }
    }
}

void sendTestData() {
    uint8_t testData[] = {0xDE, 0xAD, 0xBE, 0xEF};
    
    Serial.print("Sending test data to 0x");
    Serial.print(DEST_ADDRESS, HEX);
    Serial.print(": ");
    
    for (uint8_t i = 0; i < sizeof(testData); i++) {
        Serial.print("0x");
        if (testData[i] < 0x10) Serial.print("0");
        Serial.print(testData[i], HEX);
        Serial.print(" ");
    }
    Serial.println();
    
    cdbus.send(DEST_ADDRESS, testData, sizeof(testData));
    Serial.println("Data sent");
}

void readReceivedData() {
    if (cdbus.available()) {
        uint8_t src;
        uint8_t buffer[64];
        uint8_t len = cdbus.read(&src, buffer, sizeof(buffer));
        
        Serial.print("Read data from 0x");
        Serial.print(src, HEX);
        Serial.print(": ");
        
        for (uint8_t i = 0; i < len; i++) {
            Serial.print("0x");
            if (buffer[i] < 0x10) Serial.print("0");
            Serial.print(buffer[i], HEX);
            Serial.print(" ");
        }
        Serial.println();
    } else {
        Serial.println("No data available");
    }
}

void checkAvailability() {
    bool available = cdbus.available();
    Serial.print("Data available: ");
    Serial.println(available ? "Yes" : "No");
}